package com.cortex.pineapple.utilities;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Environment;

import com.cortex.pineapple.database.VisitLink;
import com.cortex.pineapple.fetch.RequestData.SOURCE_TYPE;
import com.cortext.pineapple.link.ImageLink;
import com.cortext.pineapple.link.Link;
import com.cortext.pineapple.link.NewsLink;
import com.cortext.pineapple.link.VideoLink;
import com.cortext.pineapple.link.WebLink;

public class ParseJson {

	private static final String ID = "ID";
	private static final String TITLE = "Title";
	private static final String DESCRIPTION = "Description";
	private static final String URL = "Url";
	private static final String MEDIA_URL = "MediaUrl";
	private static final String SOURCE_URL = "SourceUrl";
	private static final String DISPLAY_URL = "DisplayUrl";
	private static final String FILE_SIZE = "FileSize";
	private static final String HEIGHT = "Height";
	private static final String WIDTH = "Width";
	private static final String CONTENT_TYPE = "ContentType";
	private static final String RUN_TIME = "RunTime";
	private static final String THUMBAIL = "Thumbnail";
	private static final String DATE = "Date";
	private static final String SOURCE = "Source";
	private static final String START_VISIT = "StartVisit";
	private static final String END_VISIT = "EndVisit";
	private static final String TYPE = "SourceType";
	private static final String ID_SEASON = "idSeason";

	public static String readFromFile(String nameFile) throws IOException{
		StringBuilder text = new StringBuilder();
		BufferedReader br = null;
		try{
			File sdcard = Environment.getExternalStorageDirectory();

			//Get the text file
			File file = new File(sdcard,nameFile);

			//Read text from file


			br = new BufferedReader(new FileReader(file));
			String line;

			while ((line = br.readLine()) != null) {
				text.append(line);
				text.append('\n');
			}
		}
		finally{
			br.close();
		}
		return text.toString();

	}
	public static JSONArray parseResults(String result) throws JSONException{
		JSONObject json = new JSONObject(result);
		JSONObject d = json.getJSONObject("d");
		JSONArray results = d.getJSONArray("results");
		return results;
	}
	public static JSONObject parseLink(Link link) throws JSONException{
		JSONObject jsonLink = new JSONObject();
		jsonLink.put(ID, link.getId());
		jsonLink.put(TITLE, link.getTitle());
		jsonLink.put(URL, link.getUrl());
		jsonLink.put(TYPE, link.getSourceType());
		return jsonLink;
	}

	public static JSONObject parseWebLink(WebLink web) throws JSONException{
		JSONObject jsonWeb = parseLink(web);
		jsonWeb.put(DESCRIPTION, web.getDescription());
		return jsonWeb;
	}
	public static WebLink parseWebLink(JSONObject web) throws JSONException{
		String id,title,description,url;
		id = web.getString(ID);
		title = web.getString(TITLE);
		description = web.getString(DESCRIPTION);
		url = web.getString(URL);
		return new WebLink(id,title,description,url);
	}
	public static JSONObject parseImageLink(ImageLink image) throws JSONException{
		JSONObject jsonImage = parseLink(image);
		jsonImage.put(HEIGHT, image.getHeight());
		jsonImage.put(WIDTH, image.getWidth());
		jsonImage.put(DISPLAY_URL, image.getDisplayUrl());
		jsonImage.put(MEDIA_URL, image.getImageUrl());
		jsonImage.put(FILE_SIZE, image.getFileSize());
		jsonImage.put(CONTENT_TYPE, image.getType());
		return jsonImage;
	}
	public static ImageLink parseImageLink(JSONObject image) throws JSONException{
		String id,title,imageUrl,sourceUrl,displayUrl,type;
		float fileSize;
		int height,width;
		id = image.getString(ID);
		title = image.getString(TITLE);
		imageUrl = image.getString(MEDIA_URL);
		sourceUrl = image.getString(SOURCE_URL);
		displayUrl = image.getString(DISPLAY_URL);
		fileSize = (float) image.getDouble(FILE_SIZE);
		height = (int) image.getDouble(HEIGHT);
		width = (int) image.getDouble(WIDTH);
		type = image.getString(CONTENT_TYPE);
		return new ImageLink(id,title,sourceUrl,imageUrl,displayUrl,fileSize,width,height,type);
	}
	public static VideoLink parseVideoLink(JSONObject video) throws JSONException{
		String id,title,videoUrl,imageUrl;
		double fileSize,height,width,runTime;
		JSONObject thumbnail;
		id = video.getString(ID);
		title = video.getString(TITLE);
		videoUrl = video.getString(MEDIA_URL);
		runTime = video.getDouble(RUN_TIME);
		thumbnail = new JSONObject(video.getString(THUMBAIL));
		fileSize = thumbnail.getDouble(FILE_SIZE);
		height = thumbnail.getDouble(HEIGHT);
		width = thumbnail.getDouble(WIDTH);
		imageUrl = thumbnail.getString(MEDIA_URL);
		return new VideoLink(id,title,videoUrl,runTime,imageUrl,fileSize,width,height);
	}
	public static JSONObject parseVideoLink(VideoLink video) throws JSONException{
		JSONObject jsonVideo = parseLink(video);
		jsonVideo.put(HEIGHT, video.getHeight());
		jsonVideo.put(WIDTH, video.getWidth());
		jsonVideo.put(RUN_TIME, video.getRunTime());
		jsonVideo.put(MEDIA_URL, video.getImageUrl());
		jsonVideo.put(FILE_SIZE, video.getFileSize());
		return jsonVideo;
	}
	public static NewsLink parseNewsLink(JSONObject news) throws JSONException{
		String id,title,url,description,source,date;
		id = news.getString(ID);
		title = news.getString(TITLE);
		url = news.getString(URL);
		description = news.getString(DESCRIPTION);
		source = news.getString(SOURCE);
		date = news.getString(DATE);
		return new NewsLink(id,title,url,description,source,date);
	}
	public static JSONObject parseNewsLink(NewsLink news) throws JSONException{
		JSONObject jsonNews = parseLink(news);
		jsonNews.put(DATE, news.getDate());
		jsonNews.put(DESCRIPTION, news.getDescription());
		jsonNews.put(MEDIA_URL, news.getImageUrl());
		jsonNews.put(SOURCE, news.getSource());
		return jsonNews;
	}
	public static List<WebLink> parseWebLinks(JSONArray webJson) throws JSONException{
		List<WebLink> links = new ArrayList<WebLink>();
		for (int i = 0; i < webJson.length(); i++) {
			links.add(ParseJson.parseWebLink(webJson.getJSONObject(i)));
		}
		return links;
	}
	public static List<ImageLink> parseImageLinks(JSONArray imageJson) throws JSONException{
		List<ImageLink> links = new ArrayList<ImageLink>();
		for (int i = 0; i < imageJson.length(); i++) {
			links.add(ParseJson.parseImageLink(imageJson.getJSONObject(i)));
		}
		return links;
	}
	public static List<VideoLink> parseVideoLinks(JSONArray videoJson) throws JSONException{
		List<VideoLink> links = new ArrayList<VideoLink>();
		for (int i = 0; i < videoJson.length(); i++) {
			links.add(ParseJson.parseVideoLink(videoJson.getJSONObject(i)));
		}
		return links;
	}
	public static List<NewsLink> parseNewsLinks(JSONArray newsJson) throws JSONException{
		List<NewsLink> links = new ArrayList<NewsLink>();
		for (int i = 0; i < newsJson.length(); i++) {
			links.add(ParseJson.parseNewsLink(newsJson.getJSONObject(i)));
		}
		return links;
	}

	public static VisitLink parseVisitLink(String visit) throws JSONException{
		JSONObject visitLinkJson = new JSONObject(visit);
		Date enterVisit = new Date(),exitVisit = new Date();;
		long id,idSeason;
		SOURCE_TYPE type;
		String title,description,url;
		
		id = visitLinkJson.getLong(ID);
		idSeason = visitLinkJson.getLong(ID_SEASON);
		type = SOURCE_TYPE.values()[visitLinkJson.getInt(TYPE)];
		title = visitLinkJson.getString(TITLE);
		description = visitLinkJson.getString(DESCRIPTION);
		url = visitLinkJson.getString(URL);
		enterVisit.setTime(Date.parse(visitLinkJson.getString(START_VISIT)));
		exitVisit.setTime(Date.parse(visitLinkJson.getString(END_VISIT)));
		
		return new VisitLink(id, idSeason, 
				enterVisit, url, title, description, type,false,null,null);
	}
	public static String parseVisitLink(VisitLink visit) throws JSONException{
		JSONObject visitLinkJson = new JSONObject();
		visitLinkJson.put(ID, visit.getId());
		visitLinkJson.put(ID_SEASON, visit.getIdSeason());
		visitLinkJson.put(TYPE, visit.getSource().ordinal());
		visitLinkJson.put(TITLE, visit.getTitle());
		visitLinkJson.put(DESCRIPTION, visit.getDescription());
		visitLinkJson.put(URL, visit.getDescription());		
		visitLinkJson.put(START_VISIT, visit.getStartVisit());
		return visitLinkJson.toString();
	}
}
